package com.hottv.meeting.videomeeting.module.meeting.view;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.herewhite.sdk.WhiteBroadView;
import com.hottv.meeting.videomeeting.R;
import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.module.agora.AgoraUtils;
import com.hottv.meeting.videomeeting.module.agora.data.MsgItem;
import com.hottv.meeting.videomeeting.module.meeting.MeetUtils;
import com.hottv.meeting.videomeeting.module.meeting.MeetingManager;
import com.hottv.meeting.videomeeting.module.meeting.bean.HotMessage;
import com.skw.library.focus.AbsFocusEffectView;

import java.util.List;

/**
 * Created by chengwenqiang on 2019/7/12.
 */

public class VideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public interface MyItemClickListener{
        void onItemClick(View view, int position);
    }

    //视频item
    public class ViewVideoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView mTvTitle;
        private RelativeLayout mVideoPanel;

        private MyItemClickListener mListener;
        public ViewVideoHolder(View itemView, MyItemClickListener listener) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.txt_name);
            mVideoPanel = (RelativeLayout) itemView.findViewById(R.id.video_space);
            mListener = listener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(mListener!=null)
            {
                mListener.onItemClick(v,getLayoutPosition());
            }
        }
    }

    //白板item
    public class ViewWhiteHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView mTvTitle;
        private WhiteBroadView mWhitePanel;
        private ImageView mImgMusk;
        private MyItemClickListener mListener;
        public ViewWhiteHolder(View itemView, MyItemClickListener listener) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.txt_white_name);
            mWhitePanel = (WhiteBroadView) itemView.findViewById(R.id.white_content_item);
            mWhitePanel.setClickable(false);
            mWhitePanel.setFocusable(false);
            mListener = listener;
            itemView.setClickable(true);
            itemView.setFocusable(true);
            itemView.setOnClickListener(this);
        }

        public ImageView getItemView()
        {
            return mImgMusk;
        }

        @Override
        public void onClick(View v) {
            if(mListener!=null)
            {
                mListener.onItemClick(v,getLayoutPosition());
            }
        }
    }

    //邀请item
    public class ViewInviteHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private MyItemClickListener mListener;
        public ViewInviteHolder(View itemView, MyItemClickListener listener) {
            super(itemView);
            mListener = listener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(mListener!=null)
            {
                mListener.onItemClick(v,getLayoutPosition());
            }
        }
    }



    private MyItemClickListener mItemClickListener;

    private List<VideoData> mVideoList;

    private WhiteBroadView mWhitePanel;


    public VideoAdapter(Context context, List<VideoData> videoList,MyItemClickListener listener) {
        super();
        this.mVideoList = videoList;
        this.mItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        // 返回数据集合大小
        return mVideoList.size();
    }

    public void setList(List<VideoData> list)
    {
        mVideoList  = list;

        notifyDataSetChanged();
    }

    public void updateData(VideoData data,int position)
    {
        mVideoList.remove(position);
        mVideoList.add(position,data);

        notifyItemChanged(position);
    }

    public void removeData(int position)
    {
        mVideoList.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        VideoData data = mVideoList.get(position);
        if(holder instanceof ViewVideoHolder){
            Log.i("MeetingTag","onBindViewHolder ViewVideoHolder  positon:"+position);
            String nickName = "";
            HotMessage.UserBean userBean = MeetingManager.getInstance().getUserBean(data.userId);
            if(userBean!=null)
            {
                nickName = userBean.getNickname();
            }
            ((ViewVideoHolder) holder).mTvTitle.setText(MeetUtils.getUserType(data.userType)+":"+nickName);
            holder.itemView.setTag(data.viewType);
            SurfaceView surfaceView = MeetingManager.getInstance().getSurfaceViewById(data.userId);
            if(surfaceView==null)
            {
                Log.e(MeetingConfig.TAG,"------------MeetingConfig----surfaceView is null--------------");
            }
            else
            {
                AgoraUtils.checkSurfaceView(surfaceView);
                ((ViewVideoHolder) holder).mVideoPanel.removeAllViews();
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
                ((ViewVideoHolder) holder).mVideoPanel.addView(surfaceView,layoutParams);
            }

            MeetUtils.addClickNormalBox(holder.itemView,true,false);
        } else if(holder instanceof ViewWhiteHolder){
            Log.i("MeetingTag","onBindViewHolder ViewWhiteHolder  positon:"+position);
            holder.itemView.setTag(data.viewType);
            mWhitePanel = ((ViewWhiteHolder) holder).mWhitePanel;
            MeetUtils.addClickNormalBox(holder.itemView,true,false);
        } else if(holder instanceof ViewInviteHolder){
            holder.itemView.setTag(data.viewType);
        }
    }

    public WhiteBroadView getWhitePanel()
    {
        return  mWhitePanel;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if(viewType == MeetingConfig.TYPE_VIDEO)
        {
            Log.i("MeetingTag","onCreateViewHolder TYPE_VIDEO");
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, parent, false);
            MeetUtils.addClickNormalBox(view,true,false);
            return new ViewVideoHolder(view,this.mItemClickListener);
        }
        else if(viewType == MeetingConfig.TYPE_WHITE)
        {
            Log.i("MeetingTag","onCreateViewHolder TYPE_WHITE");
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_white, parent, false);
            return new ViewWhiteHolder(view,this.mItemClickListener);
        }
        else if(viewType == MeetingConfig.TYPE_INVITE)
        {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_friend, parent, false);
            view.setFocusable(true);
            return new ViewInviteHolder(view,this.mItemClickListener);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        return mVideoList.get(position).viewType;
    }

    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
